package com.yy.QSManage.global;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yy.QSManage.interceptor.AuthorizationInterceptor;
import com.yy.QSManage.interceptor.LogTraceInterceptor;
import com.yy.QSManage.tool.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;
import java.util.List;

@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Autowired
    private LogTraceInterceptor logTraceInterceptor;

    @Value("${file.upload-dir}")
    private String uploadDir;

    @Bean
    public MyGlobalCorsConfig corsConfigurer() {
        return new MyGlobalCorsConfig();
    }
    @Bean
    public AuthorizationInterceptor authorization(){ return new AuthorizationInterceptor(); }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(logTraceInterceptor).addPathPatterns("/**");
        registry.addInterceptor(corsConfigurer()).addPathPatterns("/**");
        registry.addInterceptor(authorization()).addPathPatterns("/**")
                .excludePathPatterns("/FaSysMemberApi/login","/FaSysMemberApi/logout", "/static/**", "/index.html", "/favicon.ico", "/error")
                .excludePathPatterns("/swagger-resources");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new ByteArrayHttpMessageConverter());
        converters.add(new StringHttpMessageConverter());
        converters.add(new ResourceHttpMessageConverter());
        converters.add(new AllEncompassingFormHttpMessageConverter());
        converters.add(new StringHttpMessageConverter());
        converters.add(jackson2HttpMessageConverter());
    }

    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();

        //日期格式转换
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat(DateTool.DATE_TIME_PATTERN));
//        mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));

//        //Long类型转String类型
//        SimpleModule simpleModule = new SimpleModule();
//        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
//        mapper.registerModule(simpleModule);

        converter.setObjectMapper(mapper);
        return converter;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploads/**")  // 前端访问前缀
                .addResourceLocations("file:" + uploadDir);
    }
}
